# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
@Author : Ayao
@Time   : 2022/5/18
@File   : 反转字符串中的单词 III.py
"""
"""
https://leetcode.cn/leetbook/read/array-and-string/c8su7/
"""


class Solution:
    def reverseWords(self, s: str) -> str:
        length = len(s)
        slow = 0
        res = ""
        for fast in range(length):
            # 判断末尾
            if fast == length - 1:
                res += s[slow:fast + 1][::-1]
            # 遇空格反转字符串，加上原空格，移动slow指针
            if s[fast] == " ":
                res += s[slow:fast][::-1]
                res += s[fast]
                slow = fast + 1
        return res


if __name__ == '__main__':
    print(Solution().reverseWords("God Ding"))
